package puj.news.web;

//import java.io.BufferedWriter;
//import java.io.File;
//import java.io.FileWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
//import java.io.PrintWriter;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import newsrecommendation.mediator.CommunicationMediator;
import newsrecommendation.mediator.NewsTileResponse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import puj.news.dto.Noticia;
import puj.news.util.General;

import com.google.gson.Gson;

//import puj.html.boletin.XtractNews.tipoNotas;

/**
 * Servlet implementation class Extractnews
 */
@WebServlet("/extractnews") 
public class Extractnews extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public static CommunicationMediator mediador = CommunicationMediator.getInstance();
	public static HashMap noticiasRepo = new HashMap();
	
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Extractnews() {
        super();
        
      /*  try {
			
		} catch (Exception e) {
			e.printStackTrace();
		}*/
        
        
        // TODO Auto-generated constructor stub
                
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			
			
			String idusuario = " ";

			try {
				idusuario = (String) request.getParameter("usuario");
				
				System.out.println("USUARIO> "+idusuario);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			request.setCharacterEncoding("UTF-8");
			
			response.setCharacterEncoding("UTF-8");
			
			response.setContentType("application/json");
			
			//response.getOutputStream().println("Cantidad de Noticias Encontradas: "+NewsReader.readNews(response.getOutputStream()).size());
			
			String path= this.getServletContext().getRealPath("")+File.separator;
			
			System.out.println(path);
			
			mediador.loadTestData(path);
			
			mediador.loadNews();
			
			List <NewsTileResponse>noticiasResponse=mediador.getOrganizedNews(idusuario);//NewsReader.readNews();
			List<Noticia> noticias = new ArrayList<Noticia>();
			
			for(int i =0; i<noticiasResponse.size(); i++){
				noticiasResponse.get(i).getNoticia().setIsTop(String.valueOf(noticiasResponse.get(i).isIsPopular()));
				System.out.println(noticiasResponse.get(i).isIsPopular());
				noticias.add(noticiasResponse.get(i).getNoticia());
				Noticia tmp=(Noticia)noticiasResponse.get(i).getNoticia().clone();
				noticiasRepo.put(tmp.getId(), tmp);
			}
			
			Gson gson = new Gson();
			
			String json = gson.toJson(General.clearDetails(noticias));
			
			//Charset characterSet = Charset.forName("US-ASCII");
			
			response.getOutputStream().println(json);
			//new String(json.getBytes(characterSet))
			
			
		} catch (Exception e) {			
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
